package net.cyan.crud.view.html;

import net.cyan.commons.util.*;
import net.cyan.crud.CrudConfig;
import net.cyan.crud.view.components.ValueComponent;

import java.util.*;

/**
 * 
 * @author camel
 * @date 2009-8-14
 */
public abstract class ValueComponentHtmlUI<C extends ValueComponent> extends AbstractComponentHtmlUI<C>
{
    public static final String HTMLFIELDDECORATOR = HtmlFieldDecorator.class.getName();

    public ValueComponentHtmlUI()
    {
    }

    protected Map<String, String> getProperties(ValueComponent component, String tagName, Object obj,
                                                Set<String> EXCLUSIONS) throws Exception
    {
        TypeValue typeValue = component.getTypeValue(obj);
        Object value = typeValue == null ? component.getValue(obj) : typeValue.getValue();

        Map<String, String> properties = HtmlUIUtils.getProperties(component, obj, EXCLUSIONS);

        if (properties == null)
            properties = new HashMap<String, String>();

        properties.put("name", component.getExpression());

        if (value != null)
            properties.put("value", DataConvert.toString(value));

        HtmlFieldDecorator decorator = getDecorator();
        if (decorator != null)
            decorator.decorate(tagName, component.getExpression(), typeValue, properties);

        return properties;
    }

    public static void setDecorator(HtmlFieldDecorator decorator)
    {
        CrudConfig.setConfig(HTMLFIELDDECORATOR, decorator);
    }

    public HtmlFieldDecorator getDecorator()
    {
        return (HtmlFieldDecorator) CrudConfig.getConfig(HTMLFIELDDECORATOR);
    }
}
